The straight-line distance between two points, in pixels - dir_to’s own partner for anything that needs to know both which way and how far.
dist_to(x1, y1, x2, y2) -> real| Name | Type |
|---|---|
x1 | real |
y1 | real |
x2 | real |
y2 | real |
real
The straight-line distance between two points, in pixels - dir_to's own partner for anything that needs to know both which way and how far.
The four numbers may be places (counted from the room's top-left corner, like an instance's x and y) or they may be amounts (dist_to(0, 0, dx, dy) measures how long the arrow (dx, dy) is). When both points are real positions, such as dist_to(x, y, other.x, other.y), only the difference between them matters, so it does not matter which corner the counting starts from. A fixed place written as a number is different: dist_to(0, 0, x, y) now measures from the room's top-left corner, where it used to measure from the middle of the room. The converter cannot tell that number from an amount, so it lists such calls for you to check by hand.


target = instance_nearest(x, y, obj_doc_walker);
if (target != nothing) {
direction = dir_to(x, y, target.x, target.y);
var range = dist_to(x, y, target.x, target.y);
}The same turret-aiming setup dir_to's own example uses, extended by one line: direction says which way to aim, range says how far the target actually is - close enough to fire, or still worth closing the distance first.
dist_to counts fractions of a pixel, the same as dir_to: 0.7 across and 0.3 down is a distance of about 0.76, not 0. It also stays safe on big distances. Squaring a distance directly in real (the textbook sqrt(dx*dx + dy*dy), by hand) overflows past a few hundred pixels, because real only has about 20 bits for its whole-number part. dist_to squares whole numbers instead, so it is safe across a GBA room and far beyond. See sqrt for the full account of that trap if the formula is ever needed by hand instead.
Standing exactly on the target returns 0.
Counts fractions of a pixel (down to 1/4096), the same as dir_to. It does not square in real (the textbook sqrt(dx*dx + dy*dy)), which silently overflows real’s ~524,000-unit range past a few hundred pixels: it squares whole numbers of 1/4096ths (scaled down together when the distance is big), so it is safe across a GBA room and far beyond. Standing exactly on the target returns 0.